
package com.jf.cloud.delivery.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.delivery.vo.AreaVO;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.delivery.dto.AreaDTO;
import com.jf.cloud.delivery.mapper.AreaMapper;
import com.jf.cloud.delivery.model.Area;
import com.jf.cloud.delivery.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 省市区地区信息
 *
 * @author zz
 * @date 2020-11-25 14:48:52
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaVO> list(AreaDTO areaDTO) {
        return areaMapper.list(areaDTO);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.AREA_INFO_KEY, key = "'areaList'", sync = true)
    public List<AreaVO> getAreaListInfo() {
        List<AreaVO> list = areaMapper.getAreaListInfo();
        Map<Long, List<AreaVO>> areaMap = list.stream().collect(Collectors.groupingBy(AreaVO::getParentId));
        List<AreaVO> regionList = areaMap.get(0L);
        if (CollUtil.isEmpty(regionList)) {
            return new ArrayList<>();
        }
        List<AreaVO> areaList = new ArrayList<>();

        Iterator<AreaVO> iterator = regionList.iterator();
        for (AreaVO areaVO : regionList) {
            List<AreaVO> provinceList = areaMap.get(areaVO.getAreaId());
            extracted(provinceList, areaMap);
            if (CollUtil.isEmpty(provinceList)) {
                iterator.remove();
                continue;
            }
            for (AreaVO province : provinceList) {
                List<Long> cityIds = new ArrayList<>();
                for (AreaVO city : province.getAreas()) {
                    cityIds.add(city.getAreaId());
                    List<Long> areaIds = new ArrayList<>();
                    for (AreaVO area : city.getAreas()) {
                        areaIds.add(area.getAreaId());
                    }
                    city.setAreaIds(areaIds);
                }
                province.setAreaIds(cityIds);
            }
            areaList.addAll(provinceList);
        }

        return areaList;
    }

    @Override
    @Cacheable(cacheNames = CacheNames.AREA_INFO_KEY, key = "'listAreaOfEnable'", sync = true)
    public List<AreaVO> listAreaOfEnable() {
        List<AreaVO> list = areaMapper.listAreaOfEnable();
        Map<Long, List<AreaVO>> areaMap = list.stream().collect(Collectors.groupingBy(AreaVO::getParentId));
        list = list.stream().filter(areaVO -> Objects.equals(areaVO.getLevel(), 1)).collect(Collectors.toList());
        extracted(list, areaMap);
        return list;
    }

    private void extracted(List<AreaVO> list, Map<Long, List<AreaVO>> areaMap) {
        Iterator<AreaVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            AreaVO areaVO = iterator.next();
            if (!areaMap.containsKey(areaVO.getAreaId())) {
                iterator.remove();
                continue;
            }
            List<AreaVO> cityList = new ArrayList<>();
            List<Long>  areaids = new ArrayList<>();
            for (AreaVO city : areaMap.get(areaVO.getAreaId())) {
                if (!areaMap.containsKey(city.getAreaId())) {
                    continue;
                }
                city.setAreas(areaMap.get(city.getAreaId()));
                List<Long> areaIdsList = new ArrayList<>();
                city.getAreas().forEach(s->areaIdsList.add(s.getAreaId()));
                city.setAreaIds(areaIdsList);
                cityList.add(city);
                areaids.add(city.getAreaId());
            }
            if (CollUtil.isEmpty(cityList)) {
                iterator.remove();
                continue;
            }
            areaVO.setAreas(cityList);
            areaVO.setAreaIds(areaids);
        }
    }

    @Override
    public Integer countByNameAndParentId(String areaName, Integer level, Long parentId, Long areaId) {
        return areaMapper.countByNameAndParentId(areaName,level, parentId, areaId);
    }

    @Override
    public AreaVO getByAreaId(Long areaId) {
        return areaMapper.getByAreaId(areaId);
    }

    @Override
    public AreaVO getByAreaName(String areaName) {
        return areaMapper.getByAreaName(areaName);
    }

    @Override
    public void save(Area area) {
        areaMapper.save(area);
    }

    @Override
    public void update(Area area) {
        areaMapper.update(area);
    }

    @Override
    public void deleteById(Long areaId) {
        int areaNum = areaMapper.countByAreaId(areaId);
        if (areaNum > 0) {
            throw new LuckException("请先删除子地区");
        }
        areaMapper.deleteById(areaId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.AREA_KEY, key = "'list:' + #pid")
    public List<AreaVO> listByPid(Long pid) {
        return areaMapper.listByPid(pid);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheNames.AREA_INFO_KEY, key = "'areaList'"),
            @CacheEvict(cacheNames = CacheNames.AREA_INFO_KEY, key = "'listAreaOfEnable'"),
            @CacheEvict(cacheNames = CacheNames.AREA_KEY, key = "'list:' + #pid")
    })
    public void removeAllCache(Long pid) {

    }

    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheNames.AREA_KEY, key = "'list:' + #pid"),
            @CacheEvict(cacheNames = CacheNames.AREA_INFO_KEY, key = "'areaList'"),
            @CacheEvict(cacheNames = CacheNames.AREA_INFO_KEY, key = "'listAreaOfEnable'")
    })
    public void removeAreaCacheByParentId(Long pid) {

    }
}
